package com.example.cameratest;

import android.Manifest;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ImageFormat;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.tao.camera.CameraListener;
import com.tao.camera.CameraManager;
import com.tao.camera.CameraOpHelper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {


    private View tv_rgb;
    private View tv_ir;
    private ImageView iv_rgb;
    private SurfaceView tv_rgb_2;
    private ImageView iv_ir;
    private SlideTextView stv_str;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.CAMERA}, 100);
        }

        tv_rgb = findViewById(R.id.tv_rgb);
        tv_ir = findViewById(R.id.tv_ir);
        iv_rgb = findViewById(R.id.iv_rgb);
        tv_rgb_2 = findViewById(R.id.tv_rgb_2);
        iv_ir = findViewById(R.id.iv_ir);
        stv_str = findViewById(R.id.stv_str);

        init(null);
        stv_str.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stv_str.setText(stv_str.getText().toString() + "-12345689-");
            }
        });
        stv_str.startFor0();


//        testSwitch();
    }

    private void testSwitch() {
        stv_str.postDelayed(new Runnable() {
            @Override
            public void run() {
                switchc(null);
                testSwitch();
            }
        }, 3000);
    }

    private String tag = getClass().getName();

    public void init(View view) {

        CameraListener frontListener = new CameraListener() {


            @Override
            public void onCameraOpened(Camera camera, int cameraId, int displayOrientation, boolean isMirror) {
                Log.e(tag, "frontListener onCameraOpened " + cameraId);

            }

            @Override
            public void onPreview(byte[] data, Camera camera) {
//                Log.e(tag, "frontListener onPreview ");
//                int measuredHeight = tv_rgb_2.getMeasuredHeight();
//                int measuredWidth = tv_rgb_2.getMeasuredWidth();
//
//                if (measuredHeight == 0)
//                    return;
//
//                SurfaceHolder holder = tv_rgb_2.getHolder();
//                Canvas canvas = holder.lockCanvas();
//                try {
//                    if (canvas == null)
//                        return;
//                    canvas.save();
//                    Camera.Size previewSize = camera.getParameters().getPreviewSize();
//                    Bitmap bitmap = nv212Bitmap(data, previewSize.width, previewSize.height);
//                    if (bitmap == null)
//                        return;
//                    canvas.drawBitmap(bitmap, new Rect(0, 0, measuredWidth, measuredHeight), new RectF(0, 0, measuredWidth, measuredHeight), new Paint());
//                    canvas.restore();
//                } finally {
//                    if (canvas != null)
//                        holder.unlockCanvasAndPost(canvas);
//                }
            }

            @Override
            public void onCameraClosed() {
                Log.e(tag, "frontListener onCameraClosed ");

            }

            @Override
            public void onCameraError(Exception e) {
                Log.e(tag, "frontListener onCameraError " + e.toString());
            }

            @Override
            public void onCameraConfigurationChanged(int cameraID, int displayOrientation) {
                Log.e(tag, " " + cameraID);
            }

            @Override
            public void onTakePic(byte[] jpeg, int width, int height) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length);

                Log.e(tag, "frontListener onTakePic " + bitmap);

                iv_ir.setImageBitmap(bitmap);

            }

            @Override
            public void onFaceDetection(Camera.Face[] faces, Camera camera) {

            }
        };
        CameraListener backListener = new CameraListener() {
            @Override
            public void onCameraOpened(Camera camera, int cameraId, int displayOrientation, boolean isMirror) {
                Log.e(tag, "backListener onCameraOpened " + cameraId);

            }

            @Override
            public void onPreview(byte[] data, Camera camera) {
//                Log.e(tag, "backListener onPreview " );
//                Log.e(tag, "backListener onPreview ");

            }

            @Override
            public void onCameraClosed() {
                Log.e(tag, "backListener onCameraClosed ");

            }

            @Override
            public void onCameraError(Exception e) {
                Log.e(tag, "backListener onCameraError ");

            }

            @Override
            public void onCameraConfigurationChanged(int cameraID, int displayOrientation) {
                Log.e(tag, "backListener onCameraConfigurationChanged ");

            }

            @Override
            public void onTakePic(byte[] jpeg, int width, int height) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length);
                Log.e(tag, "backListener onTakePic " + bitmap);
                iv_rgb.setImageBitmap(bitmap);
            }

            @Override
            public void onFaceDetection(Camera.Face[] faces, Camera camera) {
            }
        };

        CameraOpHelper.instance().initCameraPer(getApplicationContext(), new Point(640, 480));

        CameraOpHelper.instance().switchCameraPerviewView(tv_rgb, true);
        CameraOpHelper.instance().switchCameraPerviewView(tv_ir, false);

        CameraOpHelper.instance().addCameraListener(true, frontListener);
        CameraOpHelper.instance().addCameraListener(false, backListener);

    }


    private Bitmap nv212Bitmap(byte[] cacheData, int width, int height) {
        if (cacheData == null)
            return null;
        Bitmap bitmap = null;
        YuvImage image = new YuvImage(cacheData, ImageFormat.NV21, width, height, null);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        image.compressToJpeg(new Rect(0, 0, width, height), 100, stream);
        bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
        try {
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }


    boolean sh = false;

    public void switchc(View view) {
        sh = !sh;
        CameraOpHelper.instance().switchCameraPerviewView(tv_ir, sh);
        CameraOpHelper.instance().switchCameraPerviewView(tv_rgb, !sh);

        startp(null);
    }

    public void startp(View view) {
        CameraOpHelper.instance().startPerview(true);
        CameraOpHelper.instance().startPerview(false);
    }

    public void stopp(View view) {
        CameraOpHelper.instance().stopPerview(true);
        CameraOpHelper.instance().stopPerview(false);
    }

    public void release(View view) {
        CameraOpHelper.instance().releaseCamera(true);
        CameraOpHelper.instance().releaseCamera(false);
    }

    public void takePic(View view) {
        CameraOpHelper.instance().takePic(false);
        CameraOpHelper.instance().takePic(true);
    }

    public void clear(View view) {
        CameraOpHelper.instance().clearCameraListener(true);
        CameraOpHelper.instance().clearCameraListener(false);
    }
}